package com.example.drawdog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.Nullable;
public class DrawView extends View {
    public DrawView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        this.setBackgroundResource(R.drawable.bg);
        Paint paint=new Paint();                 //创建画笔
        paint.setColor(Color.RED);               //设置画笔颜色为红色
        //画实心矩形
        paint.setStyle(Paint.Style.STROKE);      //设置画笔样式为填充
        paint.setStrokeWidth(8);                 //设置描边的宽度为8像素
        Rect rect=new Rect(40,40,300,200);       //创建Rect对象（矩形），指定左上角坐标和右下角坐标
        canvas.drawRect(rect,paint);             //画矩形
        //画空心圆
        paint.setStyle(Paint.Style.FILL);        //设置画笔样式为描边
        canvas.drawCircle(500,120,100, paint);   //画一个圆，指定圆心坐标（500,120）和半径100
        //画椭圆
        paint.setStyle(Paint.Style.STROKE);      //设置画笔样式为描边
        paint.setStrokeWidth(8);                 //设置描边的宽度为8像素
        RectF rectF=new RectF(700,40,980,200);   //创建RectF(浮点数)，指定左上角坐标和右下角坐标
        canvas.drawOval(rectF,paint);            //画一个椭圆

        //写字
        paint.setStrokeWidth(3);
        paint.setTextSize(60);                   //设置画笔的字体大小
        paint.setColor(Color.YELLOW);            //设置画笔颜色为黄色
        canvas.drawText("雪地里的小狗",320,350, paint); //使用画笔写文字，文字左侧基线坐标（300,350)
        //划直线
        paint.setColor(Color.GREEN);              //设置画笔颜色为绿色
        canvas.drawLine(40,400,1000,400,paint);    //画一条起点（40,400)到终点（600,400）的直线
        //资源文件中获取一个图片，转换为位图对象dogBitmap
        Bitmap dogBitmap= BitmapFactory.decodeResource(getContext().getResources(),R.drawable.dog);
        canvas.drawBitmap(dogBitmap, 300,550,paint);  //在画布上显示位图，左上角坐标（300,550）
    }
}
